package cn.chia.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.UUID;

public class FileUtils {

	public static String saveFile(File orgFile, String savePath, String orgFileName) throws Exception{

		String fileName = UUID.randomUUID().toString();
		String extName = orgFileName.substring(orgFileName.lastIndexOf("."));
		
		FileOutputStream os = new FileOutputStream(new File(savePath + fileName + extName));

		FileInputStream is = new FileInputStream(orgFile);
		byte[] data = new byte[is.available()];
		
		int length = 0;
		try {
			while((length = is.read(data))!=-1){
				os.write(data, 0, length);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(is != null){
				is.close();
			}
			if(os != null){
				os.close();
			}
		}
		return fileName + extName;
	}
	
	public static void deleteFile(String filePath, String fileName){
		File file = new File(filePath + fileName);
		if(file.exists()){
			file.delete();
		}
	}
}
